package com.lzy.webapi.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.SysUserRoleServiceApi;
import com.lzy.base.dtos.SysUserRoleDto;
import com.lzy.common.apis.ParamServiceApi;
import com.lzy.common.constants.CommonConstants;
import com.lzy.common.dtos.ParamBaseDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.member.apis.MemberUserRoleServiceApi;
import com.lzy.member.apis.MemberUserServiceApi;
import com.lzy.member.dtos.MemberUserDto;
import com.lzy.member.dtos.MemberUserRoleDto;
import com.lzy.product.apis.*;
import com.lzy.product.dtos.*;
import com.lzy.webapi.response.MemberIdentityResponse;
import com.lzy.webapi.response.OrderCouponsDetailAppResponse;
import com.lzy.webapi.response.OrderProductDetailAppResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by WIN7 on 2017/12/05.
 */
@Component
public class OrderAppFacade {

    @Autowired
    private OrderProductServiceApi orderProductServiceApi;

    @Autowired
    private ProductInfoServiceApi productInfoServiceApi;

    @Autowired
    private OrderProjectRecordServiceApi orderProjectRecordServiceApi;

    @Autowired
    private OrderYieldRatioServiceApi orderYieldRatioServiceApi;

    @Autowired
    ParamServiceApi paramServiceApi;

    @Autowired
    MemberUserRoleServiceApi memberUserRoleServiceApi;

    @Autowired
    MemberUserServiceApi memberUserServiceApi;

    @Autowired
    ProductUserServiceApi productUserServiceApi;

    @Autowired
    SysUserRoleServiceApi userRoleServiceApi;

    /**
     * 查询产品订单列表
     *
     * @param pager
     * @param orderProductDto
     * @return
     */
    public PageInfo orderProductList(Pager pager, OrderProductDto orderProductDto) {
        PageInfo<OrderProductDto> pageInfo = orderProductServiceApi.selectList(pager, orderProductDto);
        for (OrderProductDto orderDto : pageInfo.getList()){
            if (orderDto.getType()!=null){
                switch (orderDto.getType()){
                    case 1:
                        if (orderDto.getStatus().equals(1))
                            orderDto.setStatusName("待支付");
                        if (orderDto.getStatus().equals(2))
                            orderDto.setStatusName("购买成功");
                        if (orderDto.getStatus().equals(3))
                            orderDto.setStatusName("已关闭");
                        break;
                    case 2:
                        if (orderDto.getStatus().equals(1))
                            orderDto.setStatusName("待支付");
                        if (orderDto.getStatus().equals(2))
                            orderDto.setStatusName("审核中");
                        if (orderDto.getStatus().equals(3))
                            orderDto.setStatusName("已关闭");
                        if (orderDto.getStatus().equals(4))
                            orderDto.setStatusName("补贴中");
                        if (orderDto.getStatus().equals(5))
                            orderDto.setStatusName("已到期");
                        break;
                }
            }
        }
        return pageInfo;
    }

    /**
     * 查看订单详情
     *
     * @param orderId
     * @return
     */
    public OrderProductDetailAppResponse orderProductDetail(Long orderId) {
        OrderProductDto orderProductDto = orderProductServiceApi.findById(orderId);
        //附加信息
        if (null != orderProductDto) {
            //产品信息
            ProductInfoDto productInfoDto = productInfoServiceApi.findById(orderProductDto.getProductId());
            if (productInfoDto != null) {
                orderProductDto.setProductName(productInfoDto.getName());
                orderProductDto.setProductImage(productInfoDto.getCover());
                orderProductDto.setContractUrl(productInfoDto.getOffice());
            }
            //温馨提示
            DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
            switch (orderProductDto.getStatus().intValue()) {
                case 1:
                    ParamBaseDto paramDto = paramServiceApi.queryByKey(CommonConstants.Param.ORDER_REMARK_NO_PAY);
                    if (paramDto != null) {
                        orderProductDto.setReminder(paramDto.getParamValue());
                    }
                    break;
                case 3:
                    ParamBaseDto paramDto2 = paramServiceApi.queryByKey(CommonConstants.Param.ORDER_REMARK_CLOSE);
                    if (paramDto2 != null) {
                        orderProductDto.setReminder(paramDto2.getParamValue());
                    }
                    break;
            }
        }
        OrderProductDetailAppResponse orderProductDetailAppResponse = DozerUtils.map(orderProductDto, OrderProductDetailAppResponse.class);
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        List<OrderProjectRecordDto> orderProjectList = orderProjectRecordServiceApi.selectOrderProjectRecordByOrderId(orderId);
        if (orderProjectList != null) {
            String projectStr = "";
            for (OrderProjectRecordDto oprd : orderProjectList) {
                projectStr += ((oprd.getProjectName() + oprd.getCount() + oprd.getProjectUnit()) + "  +  ");
            }
            if (projectStr.length() > 3) {
                projectStr = projectStr.substring(0, projectStr.length() - 5);//此数字根据最后加的字符串长度决定
            }
            orderProductDetailAppResponse.setProjectStr(projectStr);
        }
        return orderProductDetailAppResponse;
    }


    /**
     * 查看卡券宝订单列表
     *
     * @param pager
     * @param orderProductDto
     * @return
     */
    public PageInfo orderCouponsList(Pager pager, OrderProductDto orderProductDto) {
        PageInfo pageInfo = orderProductServiceApi.selectList(pager, orderProductDto);
        if (pageInfo != null && pageInfo.getList().size() > 0) {
            List<OrderProductDto> list = pageInfo.getList();
            for (OrderProductDto dto : list) {
                //订单选择收益比率信息
                OrderYieldRatioDto yieldRatioDto = orderYieldRatioServiceApi.findOrderYieldRatioByOrderId(dto.getOrderId());
                if (yieldRatioDto != null) {
                    dto.setOrderYieldRatio(yieldRatioDto);
                }
                //订单包含项信息
                List<OrderProjectRecordDto> recordDtoList = orderProjectRecordServiceApi.selectOrderProjectRecordByOrderId(dto.getOrderId());
                if (recordDtoList != null) {
                    dto.setProjects(recordDtoList);
                }
                //产品信息
                ProductInfoDto productInfoDto = productInfoServiceApi.findById(orderProductDto.getProductId());
                if (productInfoDto != null) {
                    orderProductDto.setProductName(productInfoDto.getName());
                    orderProductDto.setContractUrl(productInfoDto.getOffice());
                }
            }
        }
        return pageInfo;
    }

    /**
     * 查看卡券宝订单详情
     *
     * @param orderId
     * @return
     */
    public OrderCouponsDetailAppResponse orderCouponsDetail(Long orderId) {
        OrderProductDto orderProductDto = orderProductServiceApi.findById(orderId);
        //产品信息
        ProductInfoDto productInfoDto = productInfoServiceApi.findById(orderProductDto.getProductId());
        if (productInfoDto != null) {
            orderProductDto.setContractUrl(productInfoDto.getOffice());
        }
        OrderCouponsDetailAppResponse orderProductDetailAppResponse = DozerUtils.map(orderProductDto, OrderCouponsDetailAppResponse.class);
        return orderProductDetailAppResponse;
    }

    /**
     * 根据会员编号查询身份以及编号
     * @param memberId
     * @return
     */
    public MemberIdentityResponse getIdentity(Long memberId) {
        //是否卡券销售商
        MemberIdentityResponse memberIdentityResponse = new MemberIdentityResponse();
        OrderProductDto orderProductDto = new OrderProductDto();
        orderProductDto.setMemberId(memberId);
        orderProductDto.setStatus(4);//补贴中
        PageInfo pageInfo = orderProductServiceApi.selectList(null, orderProductDto);
        memberIdentityResponse.setSaleMan(0);//普通会员
        List<OrderProductDto> list = pageInfo.getList();
        if (pageInfo != null && list != null && list.size() > 0) {
            memberIdentityResponse.setSaleMan(2);//一般代理
            for (OrderProductDto productDto : list) {
                if (productDto.getMoneyCount() != null && productDto.getMoneyCount().compareTo(new BigDecimal(9999)) > 0) {
                    memberIdentityResponse.setSaleMan(1);//特约代理
                }
            }
        }
        //判断是否代理商
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        MemberUserDto memberUserDto = memberUserServiceApi.selectMemberByMemberId(memberId);
        DataSourceHolder.clearDataSource();
        if(memberUserDto != null && memberUserDto.getAccount() != null){
            DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
            ProductUserDto userDto = productUserServiceApi.queryByPhone(memberUserDto.getAccount());
            if(userDto != null){
                SysUserRoleDto userRoleDto = new SysUserRoleDto();
                userRoleDto.setUserId(userDto.getId());
                userRoleDto.setRoleId(5L);
                SysUserRoleDto sysUserRoleDto = userRoleServiceApi.selectByUserRole(userRoleDto);
                if(sysUserRoleDto != null){
                    memberIdentityResponse.setSaleMan(3);//代理销售代理商
                }
            }
            DataSourceHolder.clearDataSource();
        }
        //是否核销角色
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        memberIdentityResponse.setCheckMan(Boolean.FALSE);
        List<MemberUserRoleDto> roleDtoList = memberUserRoleServiceApi.selectListByMemberId(memberId);
        if (!roleDtoList.isEmpty()) {
            for (MemberUserRoleDto memberUserRoleDto : roleDtoList) {
                if (memberUserRoleDto.getRoleId() != null && "1".equals(memberUserRoleDto.getRoleId().toString())) {
                    memberIdentityResponse.setCheckMan(Boolean.TRUE);
                }
            }
        }
        DataSourceHolder.clearDataSource();
        memberIdentityResponse.setShareUrl("http://lzyapp.ssl.lzyhll.com/" + "share/pages/stone-forest/index.html" + "?saleMam=" + memberId);
        memberIdentityResponse.setShareBanner("http://img1.goods.lzyhll.com/FlS7rPgvrJ7LDR1Sk-DXC0yVJg0h?100/100");
        memberIdentityResponse.setShareRemark("中国首个石头上的运动小镇，用最狂野的方式去石林深处一起撒野！");
        memberIdentityResponse.setShareTitle("石林汽摩小镇，汽摩券火热发售中");
        return memberIdentityResponse;
    }

}
